package comframe.jdbc.sql.support;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.apache.log4j.Logger;

import comframe.jdbc.sql.condition.Condition;

public class GeneralBaseDao<T> extends TempleteSupport {
     Logger logger = Logger.getLogger(GeneralBaseDao.class);
	private Class<T> clazz;

	@SuppressWarnings("unchecked")
	public GeneralBaseDao() {
		this.clazz = (Class<T>) ((ParameterizedType) this.getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
	}
   /**
    * 根据主键查询对象
    * @param id
    * @return
    */
	@SuppressWarnings("unchecked")
	public T findByid(Object id) {
		try {
			return (T) this.getTemplete().findById(clazz, id);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return null;
	}
    /**
     * 根据主键删除对象
     * @param id
     * @return
     */
	public boolean removeByid(Object id) {
		try {
			return this.getTemplete().deleteById(clazz,id);
		} catch (Exception e) {
			logger.error("数据删除异常");
		}
		return false;
	}
	
	
	public boolean remove(Condition condition) {
		try {
			return this.getTemplete().delete(condition,clazz);
		} catch (Exception e) {
			logger.error("数据删除异常");
		}
		return false;
	}
	
    /**
     * 插入对象
     * @param t
     * @return
     */
	public boolean save(T t) {
		try {
			return this.getTemplete().save(t);
		} catch (Exception e) {
			logger.error("数据提交异常");
			return false;
		}
	}
    /**
     * 根据对象删除对象
     * @param t
     * @return
     */
	public boolean remove(T t) {
		try {
			return this.getTemplete().delete(t);
		} catch (Exception e) {
			logger.error("数据删除异常");
		}
		return false;
	}
   /**
    * 很据条件查询列表
    * @param condition
    * @return
    */
	@SuppressWarnings("unchecked")
	public List<T> findByCondition(Condition condition) {
		try {
			return (List<T>) this.getTemplete().find(condition, clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return null;
	}
    /**
     * 根据条件分页查询列表
     * @param condition
     * @param page
     * @param pageSize
     * @return
     */
	@SuppressWarnings("unchecked")
	public List<T> findPageByCondition(Condition condition, int page,
			int pageSize) {
		try {
			return (List<T>) this.getTemplete().PageQuery(page, pageSize,
					condition, clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return null;
	}
	/**
	 * 分页查询
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<T> findPage( int page,int pageSize) {
		try {
			return (List<T>) this.getTemplete().PageQuery(page, pageSize,
					clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return null;
	}
    /**
     * 更具对象更新
     * @param t
     * @return
     */
	public boolean update(T t) {
		try {
			return this.getTemplete().update(t);
		} catch (Exception e) {
			logger.error("数据更新异常");
		}
		return false;
	}
	/**
	 * 更具条件更新对象
	 * @param t
	 * @param condition
	 * @return
	 */
	public boolean update(T t,Condition condition) {
		try {
			return this.getTemplete().update(t,condition);
		} catch (Exception e) {
			logger.error("数据更新异常");
		}
		return false;
	}
    /**
     * 只更新对象值存在的属性
     * @param t
     * @return
     */
	public boolean updateExist(T t) {
		try {
			return this.getTemplete().updateIsExist(t);
		} catch (Exception e) {
			logger.error("数据更新异常");
		}
		return false;
	}
	/**
	 * 更具条件只更新对象值存在的属性
	 * @param t
	 * @param condition
	 * @return
	 */
	public boolean updateExist(T t,Condition condition) {
		try {
			return this.getTemplete().updateIsExist(t,condition);
		} catch (Exception e) {
			logger.error("数据更新异常");
		}
		return false;
	}
	
	/**
	 * 更具条件只更新对象值存在的属性
	 * @param t
	 * @param condition
	 * @return
	 */
	public boolean updateBySql(String sql) {
		try {
			return this.getTemplete().updateBySql(sql,clazz);
		} catch (Exception e) {
			logger.error("数据更新异常");
		}
		return false;
	}

	
	
	
	/**
	 * 通过sql查询
	 * @param sql
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<T> findBySql(String sql) {
		try {
			return (List<T>) this.getTemplete().querySql(sql, clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return null;
	}
    /**
     * 更具条件求总数
     * @param condition
     * @return
     */
	public int CountByCondition(Condition condition) {
		try {
			return this.getTemplete().count(condition, clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return 0;
	}
	
	/**
     * 更具单一条件条件求总数
     * @param condition
     * @return
     */
	public int CountByCondition(String name, Object value) {
		try {
			return this.getTemplete().count( name,  value, clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return 0;
	}
    /**
     * 获取表数量
     * @return
     */
	public int allCount() {
		try {
			return this.getTemplete().count(clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return 0;
	}
    /**
     * 更具单一条件查询
     * @param name
     * @param value
     * @return
     */
	@SuppressWarnings("unchecked")
	public List<T> findByProperty(String name, Object value) {
		Condition condition = new Condition();
		condition.createCirteria().andEq(name, value);
		try {
			return (List<T>) this.getTemplete().find(condition, clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return null;
	}
    /**
     * 模糊查询
     * @param name
     * @param value
     * @return
     */
	@SuppressWarnings("unchecked")
	public List<T> findFullLikeByProperty(String name, Object value) {
		Condition condition = new Condition();
		condition.createCirteria().andLike(name,
				value.toString());
		try {
			return (List<T>) this.getTemplete().find(condition, clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return null;
	}
    /**
     * 左边匹配查询
     * @param name
     * @param value
     * @return
     */
	@SuppressWarnings("unchecked")
	public List<T> findLeftLikeByProperty(String name, Object value) {
		Condition condition = new Condition();
		condition.createCirteria().andLeftLike(name,
				value.toString());
		try {
			return (List<T>) this.getTemplete().find(condition, clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return null;
	}
    /**
     * 右边匹配查询
     * @param name
     * @param value
     * @return
     */
	@SuppressWarnings("unchecked")
	public List<T> findRightLikeByProperty(String name, Object value) {
		Condition condition = new Condition();
		condition.createCirteria().andRightLike(name,
				value.toString());
		try {
			return (List<T>) this.getTemplete().find(condition, clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return null;
	}
    /**
     * 批量保存
     * @param list
     */
	public void saveAll(List<T> list) {
		for (T t : list) {
			this.save(t);
		}
	}
	
	
	 /**
     * 批量保存
     * @param list
     */
	public boolean saveBatch(List<T> list) {
		try {
			return this.getTemplete().saveBatch(list);
		} catch (Exception e) {
			logger.error("数据批量保存错误"+e.getMessage());
		}
		return false;
	}
	
	/**
	 * 批量删除
	 * @param list
	 */
	public void removeAll(List<T> list) {
		for (T t : list) {
			this.remove(t);
		}
	}
	
	/**
	 * 批量删除
	 * @param list
	 */
	public void removeAllById(List<Object> list) {
		for (Object id : list) {
			this.removeByid(id);
		}
	}
	/**
	 * 批量更新
	 * @param list
	 */
	public void updateAll(List<T> list) {
		for (T t : list) {
			this.update(t);
		}
	}
	
	/**
	 * 批量更新
	 * @param list
	 */
	public void updateAllIsExist(List<T> list) {
		for (T t : list) {
			this.updateExist(t);
		}
	}
	/**
	 * 查询所有
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<T> findAll(){
		try {
			return (List<T>) this.getTemplete().find(clazz);
		} catch (Exception e) {
			logger.error("数据查询异常");
		}
		return null;
	}
	/**
	 * 更具条件删除
	 * @param name
	 * @param value
	 * @return
	 */
	public boolean removeByProperty(String name,Object value){
		Condition condition = new Condition();
		condition.createCirteria().andEq(name, value);
		try {
			return this.getTemplete().delete(condition, clazz);
		} catch (Exception e) {
			logger.error("数据删除异常");
		}
		return false;
	}
}
